package com.ymm.lulutong.controller;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperOrderDomainService;
import com.ymm.lulutong.utils.Result;
import com.ymm.system.api.dto.ShipperOrderBriefResponse;
import com.ymm.system.api.dto.ShipperOrderDetailResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/order")
public class ShipperOrderController {
    @Resource
    private ShipperOrderDomainService shipperOrderDomainService;

    /**
     * 取消订单并下架货源
     *
     * @param orderId 订单id
     * @return 成功与否
     */
    @PostMapping("/cancelOrderWithPullOffCargo")
    public Result<Boolean> cancelOrderWithPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) {
        if (Objects.isNull(shipperId) || Objects.isNull(orderId)) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR);
        }
        try{
            return shipperOrderDomainService.cancelOrderWithPullOffCargo(orderId, shipperId, orderCancelReason, orderCancelDescription);
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail(false, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    /**
     * 取消订单但不下架货源
     *
     * @param orderId 订单id
     * @return 成功与否
     */
    @PostMapping("/cancelOrderWithoutPullOffCargo")
    public Result<Boolean> cancelOrderWithoutPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) {
        if (Objects.isNull(shipperId) || Objects.isNull(orderId)) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR);
        }
        try {
            return shipperOrderDomainService.cancelOrderWithoutPullOffCargo(orderId, shipperId, orderCancelReason, orderCancelDescription);
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail(false, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    @PostMapping("/confirmReceipt")
    public Result<Boolean> confirmReceipt(Long orderId, Long shipperId) {
        if (Objects.isNull(shipperId) || Objects.isNull(orderId)) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR);
        }
        try {
            return shipperOrderDomainService.confirmReceipt(orderId, shipperId);
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail(false, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    /**
     * 查看订单详细信息
     *
     * @param orderId 订单id
     * @return 订单详细信息
     */
    @GetMapping("/orderDetail")
    public Result<ShipperOrderDetailResponse> getOrderDetailByOrderId(Long orderId) {
        if (Objects.isNull(orderId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperOrderDomainService.getOrderDetailByOrderId(orderId);
    }

    /**
     * 查看进行中的订单
     *
     * @param shipperId 货主id
     * @return 进行中的货源列表
     */
    @GetMapping("/ongoingOrders")
    public Result<List<ShipperOrderBriefResponse>> getOngoingOrderByShipperId(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperOrderDomainService.listOngoingOrdersByShipperId(shipperId);
    }

    /**
     * 查看已完成的货源
     *
     * @param shipperId 货主id
     * @return 已完成的货源列表
     */
    @GetMapping("/completedOrders")
    public Result<List<ShipperOrderBriefResponse>> getCompletedOrderByShipperId(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperOrderDomainService.listCompletedOrdersByShipperId(shipperId);
    }

    /**
     * 查看已取消的货源
     *
     * @param shipperId 货主id
     * @return 已取消的货源列表
     */
    @GetMapping("/cancelledOrders")
    public Result<List<ShipperOrderBriefResponse>> getCancelledOrderByShipperId(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperOrderDomainService.listCancelledOrdersByShipperId(shipperId);
    }

    /**
     * 获取司机手机号
     *
     * @param orderId 订单Id
     * @return 司机手机号
     */
    @PostMapping("/getDriverPhoneNumber")
    public Result<String> getDriverPhoneNumber(Long orderId) {
        if (Objects.isNull(orderId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperOrderDomainService.getDriverPhoneNumber(orderId);
    }

}
